package cn.comtom.ewbs.signature.service.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

import cn.comtom.ewbs.signature.common.vo.request.Sm2Req;
import cn.comtom.ewbs.signature.service.Sm2Service;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@Scope("singleton")
public class Sm2ServiceImpl implements Sm2Service {

	@Override
	public String getByteSign(Sm2Req req) {
		req.setCmd(0);
		String params = JSON.toJSONString(req);
		String result = Sm2Service.Sm2Jni.INSTANCE.SM2_Json(params);
		log.info("getByteSign-params{},result:{}",params,result);
		String sign = (String) JSONUtil.getByPath(JSONUtil.parse(result), "sign");
		return sign;
	}

	@Override
	public boolean verifyByteSign(Sm2Req req) {
		req.setCmd(1);
		String params = JSON.toJSONString(req);
		String result = Sm2Service.Sm2Jni.INSTANCE.SM2_Json(params);
		log.info("verifyByteSign-params{},result:{}",params,result);
		String resultCode = JSONUtil.getByPath(JSONUtil.parse(result), "Result").toString();
		return "0".equals(resultCode);
	}

}
